package cn.szzsi.common.kit;


import cn.szzsi._Config;
import cn.szzsi.common.model.SiteConfig;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.weixin.sdk.api.ApiConfig;
import org.apache.commons.lang3.StringUtils;

import java.security.MessageDigest;
import java.util.List;
import java.util.Map;

/**
 * Created by Yishe on 8/7/2015.
 */
public class SessionUtil {
    private static ApiConfig CONFIG;

    private static final String OFFLINE_MESSAGE = "_offline_message_";

    public static final String FORWARD_URL = "_forward_url_";


    public static final ApiConfig getApiConfig(Controller c){
        if(CONFIG == null){
            ApiConfig ac = new ApiConfig();
            if(c != null){
                String apiid=c.getPara("apiid");
                if(StrKit.notBlank(apiid)){
                    SiteConfig baw= SiteConfig.getByApiid(apiid);
                    String token=baw.getStr("token");
                    String appId=baw.getStr("appid");
                    String appSecret=baw.getStr("appsecret");
                    ac.setToken(token);
                    ac.setAppId(appId);
                    ac.setAppSecret(appSecret);
                }else{
                    ac.setToken(_Config.p.get("token"));
                    ac.setAppId(_Config.p.get("appId"));
                    ac.setAppSecret(_Config.p.get("appSecret"));
                }
            }else{
                ac.setToken(_Config.p.get("token"));
                ac.setAppId(_Config.p.get("appId"));
                ac.setAppSecret(_Config.p.get("appSecret"));
            }
            ac.setEncryptMessage(_Config.p.getBoolean("encryptMessage",false));
            ac.setEncodingAesKey(_Config.p.get("encodingAesKey","setting it in config file"));
            CONFIG = ac;
        }
        return CONFIG;
    }

    public static final ApiConfig getApiConfig(String appId, String appSecret){
        ApiConfig ac = new ApiConfig();
        ac.setToken(_Config.p.get("token"));
        ac.setAppId(appId);
        ac.setAppSecret(appSecret);
        ac.setEncryptMessage(_Config.p.getBoolean("encryptMessage",false));
        ac.setEncodingAesKey(_Config.p.get("encodingAesKey","setting it in config file"));
        return ac;
    }

    //多商户
    /*public static final ApiConfig getApiConfig(String apiid){
        SiteConfig siteConfig= SiteConfig.getByApiid(apiid);
        ApiConfig ac = new ApiConfig();
        String token=siteConfig.getStr("token");
        String appId=siteConfig.getStr("appid");
        String appSecret=siteConfig.getStr("secret");
        ac.setToken(token);
        ac.setAppId(appId);
        ac.setAppSecret(appSecret);
        ac.setEncryptMessage(_Config.p.getBoolean("encryptMessage",false));
        ac.setEncodingAesKey(_Config.p.get("encodingAesKey","setting it in config file"));
        return ac;
    }*/
    public static final ApiConfig getApiConfig(String apiid){
        ApiConfig ac = new ApiConfig();
        String token=_Config.p.get("wecaht_token");
        String appId=_Config.p.get("wechat_appid");
        String appSecret=_Config.p.get("wechat_secret");
        ac.setToken(token);
        ac.setAppId(appId);
        ac.setAppSecret(appSecret);
        ac.setEncryptMessage(_Config.p.getBoolean("encryptMessage",false));
        ac.setEncodingAesKey(_Config.p.get("encodingAesKey","setting it in config file"));
        return ac;
    }

    //多商户
    /*public static final ApiConfig getApiConfigForB(Controller c){
        String apiid = c.getPara("apiid");
        if (StringUtils.isBlank(apiid)){
            String queryString = c.getRequest().getQueryString();
            if (StringUtils.isNotBlank(queryString)){
                String[] arr = queryString.split("&");
                for (String str : arr){
                    String[] paraArr = str.split("=");
                    if ("apiid".equals(paraArr[0])){
                        apiid=paraArr[1];
                    }
                }
            }
        }
        if(StringUtils.isNotBlank(apiid)){
            SiteConfig siteConfig= SiteConfig.getByApiid(apiid);
            System.out.println(siteConfig);
            ApiConfig ac = new ApiConfig();
            String token=siteConfig.getStr("token");
            String appId=siteConfig.getStr("appid");
            String appSecret=siteConfig.getStr("secret");
            ac.setToken(token);
            ac.setAppId(appId);
            ac.setAppSecret(appSecret);
            ac.setEncryptMessage(_Config.p.getBoolean("encryptMessage",false));
            ac.setEncodingAesKey(_Config.p.get("encodingAesKey","setting it in config file"));
            return ac;
        }else {
            return null;
        }
    }*/

    public static final ApiConfig getApiConfigForB(Controller c){
        ApiConfig ac = new ApiConfig();
        String token=_Config.p.get("wecaht_token");
        String appId=_Config.p.get("wechat_appid");
        String appSecret=_Config.p.get("wechat_secret");
        ac.setToken(token);
        ac.setAppId(appId);
        ac.setAppSecret(appSecret);
        ac.setEncryptMessage(_Config.p.getBoolean("encryptMessage",false));
        ac.setEncodingAesKey(_Config.p.get("encodingAesKey","setting it in config file"));
        return ac;
    }


    public static String getToken(String username, String password){
        try{
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            md.update(username.getBytes("UTF-8"));
            md.update(password.getBytes("UTF-8"));
            md.update(String.valueOf(System.currentTimeMillis()).getBytes("UTF-8"));
            byte[] digest = md.digest();
            StringBuffer hexstr = new StringBuffer();
            String shaHex = "";
            for(int i = 0;i < digest.length;++i){
                shaHex = Integer.toHexString(digest[i] & 255);
                if(shaHex.length() < 2){
                    hexstr.append(0);
                }
                hexstr.append(shaHex);
            }
            return hexstr.toString();
        }catch(Exception var12){
            return "asdaskjfaosfdmlsaijcalsdmwlkdjsac";
        }
    }


    /**
     * 获取离线消息
     */
    public static List<Map<String, Object>> getOffMsg(int cusId){
        List<Map<String, Object>> msgList = CacheKit.get(OFFLINE_MESSAGE,cusId);
        return msgList;
    }


}
